package com.op.mr3;

import org.json.JSONObject;

import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class AdmobAdapter {

    private AdView         mAdView;

    private RelativeLayout mLayout;

    public AdmobAdapter(GameActivity activity) {
        mAdView = new AdView(activity, AdSize.BANNER, "a153576de0e48ca");

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        //params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

        mLayout = new RelativeLayout(activity);
        mLayout.addView(mAdView, params);
        // CA98B0477E3911DB74994A02AB542810 小黄
        mAdView.loadAd(new AdRequest().addTestDevice("7045E0C9E2906A089872365B731EE6FF"));

        activity.addContentView(mLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }

    public void updateStatus(JSONObject data) {
        boolean visiable = data.optBoolean("visiable");
        if (!visiable) {
            mLayout.setVisibility(View.INVISIBLE);
            return;
        }

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        String position = data.optString("position");
        if ("top".equals(position)) {
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        } else {
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        }
        params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

        mAdView.setLayoutParams(params);
        mLayout.setVisibility(View.VISIBLE);
    }
}
